<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>评论回车发布</title>
  <style>
    .wrapper {
      min-width: 400px;
      max-width: 800px;
      display: flex;
      justify-content: flex-end;
    }

    .avatar {
      overflow: hidden;
      width: 48px;
      height: 48px;
      margin-right: 20px;
      border-radius: 50%;
      background: url(./images/avatar.jpg) no-repeat center / cover;
    }

    .wrapper textarea {
      flex: 1;
      height: 30px;
      padding: 10px;
      border-color: transparent;
      outline: none;
      resize: none;
      background: #f5f5f5;
      border-radius: 4px;
      transition: all 0.5s;
    }

    .wrapper textarea:focus {
      height: 50px;
      border-color: #e4e4e4;
      background: #fff;
    }

    .wrapper button {
      width: 70px;
      margin-left: 10px;
      border: none;
      color: #fff;
      background: #00aeec;
      border-radius: 4px;
      cursor: pointer;
    }

    .wrapper .total {
      margin-right: 80px;
      margin-top: 5px;
      color: #999;
      opacity: 0;
      transition: all 0.5s;
    }

    .list {
      display: flex;
      min-width: 400px;
      max-width: 800px;
    }

    .list .item {
      display: flex;
      width: 100%;
    }

    .list .item .info {
      flex: 1;
      border-bottom: 1px dashed #e4e4e4;
      padding-bottom: 10px;
    }

    .list .item p {
      margin: 0;
    }

    .list .item .name {
      color: #FB7299;
      font-size: 14px;
      font-weight: bold;
    }

    .list .item .text {
      padding: 10px 0;
      color: #333;
      font-size: 13px;

    }

    .list .item .time {
      color: #999;
      font-size: 12px;
    }
  </style>
</head>

<body>
  <div class="wrapper">
    <i class="avatar"></i>
    <textarea id="tx" placeholder="发一条友善的评论" rows="2" maxlength="200"></textarea>
    <button>发布</button>
  </div>
  <div class="wrapper">
    <span class="total">0/200字</span>
  </div>
  <div class="list">
    <div class="item" style="display: none;">
      <i class="avatar"></i>
      <div class="info">
        <p class="name">清风徐来</p>
        <p class="text">大家都辛苦啦，感谢各位大大的努力，能圆满完成真是太好了[笑哭][支持]</p>
        <p class="time">2099-10-10 20:29:21</p>
      </div>
    </div>
  </div>

  <script>
    // 获取元素
    let txt = document.querySelector('#tx')
    let total = document.querySelector('.total')
    let item = document.querySelector('.item')
    let btn = document.querySelector('.wrapper button')
    let text = document.querySelector('.text')

    // 封装函数
    function fn() {
      // 方法 .trim() 把字符串的 空 空格 去掉 
      // 例如： "        ".trim() = ""
      if (txt.value.trim() === '') {
        total.innerText = `0/200字`
        return
      }
      // 把表单输入的值 赋值 给评论
      text.innerText = txt.value
      // 把评论区显示
      item.style.display = 'block'
      // 重置input
      txt.value = ''
      // 重置input之后 把文字数量也重置
      total.innerText = `0/200字`
    }
    fn()

    // input 获得焦点时 显示 total
    txt.addEventListener('focus', function () {
      total.style.opacity = 1
    })

    // input 失去焦点时 隐藏 total
    txt.addEventListener('blur', function () {
      total.style.opacity = 0
    })

    // input 输入时 把文字长度给到 total
    txt.addEventListener('input', function () {
      total.innerText = `${txt.value.length}/200字`
    })

    // 点击button按钮时 显示item评论 并把输入在input的值 给到p标签
    btn.addEventListener('click', function () {
      fn()
    })

    // 回车 显示item评论
    txt.addEventListener('keyup', function (e) {
      if (e.key === 'Enter') {
        fn()
      }
    })
  </script>
</body>

</html>